package com.lcy.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcy.reggie.common.Result;
import com.lcy.reggie.entity.Employee;
import com.lcy.reggie.service.EmployeeService;
import com.lcy.reggie.mapper.EmployeeMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author ChunBaBa_
* @description 针对表【employee(员工信息)】的数据库操作Service实现
* @createDate 2023-01-16 14:44:31
*/
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>
    implements EmployeeService{


    /**
     * 用户登录
     */
    @Override
    public Result<Employee> login(HttpServletRequest request, Employee employee) {
        //1.拿到密码，进行md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.根据用户名username查询数据库，对比密码是否正确
        /*LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = getOne(queryWrapper); //因为数据库已经对user_name做了唯一索引，所以调用getOne获得唯一数据*/
        Employee emp = query().eq("username", employee.getUsername()).one();

        //3.判断查询结果
        if(emp == null){
            //3.1 不存在，返回错误信息
            return Result.error("用户不存在！");
        }

        //4.比对密码，不一致则返回错误信息
        if(!emp.getPassword().equals(password)){
            return Result.error("密码错误！");
        }

        //5.查看员工状态
        if(emp.getStatus() == 0){
            return Result.error("账号已禁用！");
        }

        //6.登录成功，将员工id存入session并返回登录结果
        request.getSession().setAttribute("employee",emp.getId());
        return Result.success(emp);
    }

    /**
     * 分页查询
     */
    @Override
    public Result<Page> page(HttpServletRequest request,Integer page, Integer pageSize, String name) {
        //1.构建分页构造器
        Page pageInfo = new Page(page,pageSize);
        //优化：可以先判断登录的用户是普通员工还是管理员
        Long id = (Long) request.getSession().getAttribute("employee");
        if(id != 1){
            //不等于1，证明为普通员工，则值不显示其他员工信息，只显示自己的信息
           query().eq("id", id).page(pageInfo);
        }
        //2.条件构造器
        //3.判断，如果name不为空，且不为空字符串，则模糊查询name
        else {
            if (StringUtils.isNotBlank(name)) {
                query().like("name", name).orderByDesc("update_time").page(pageInfo);
            }
            //4.如果name为空或者空字符串，则不查询name
            else {
                query().orderByDesc("update_time").page(pageInfo);
            }
        }
        return Result.success(pageInfo);
    }

    /**
     *  禁用和编辑功能
     * @param request
     * @param employee
     * @return
     */
    @Override
    public Result<String> update(HttpServletRequest request, Employee employee) {
        boolean result = updateById(employee);
        if(!result){
            return Result.error("修改失败！");
        }
        return Result.success("修改成功！");
    }

    /**
     * 添加员工
     */
    @Override
    public Result<String> saveEmployee(HttpServletRequest request, Employee employee) {

        /*先判断输入的用户名是否已经存在
        String username = employee.getUsername();
        Long count = query().eq("username", username).count();
        if(count != 0){
            // 已存在，直接返回错误信息
            return Result.error("用户名已存在！");
        }*/

        //1.设置初始密码 123456 并且md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        //2.获得当前登录用户的id
        Long id = (Long) request.getSession().getAttribute("employee");

        boolean result = save(employee);
        if(!result){
            return Result.error("添加失败！");
        }
        return Result.success("添加成功！");
    }
}




